package project1;

import java.util.ArrayList;
import java.util.List;

/**
 *  @author Andrew O'Connor
 *  COSC 603
 *  Project 1
 */
public class CleanInput {

    private String[] replacements;

    public CleanInput(List<Character> _filter) {
        replacements = new String[Character.MAX_VALUE + 1];

        for(int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++)
            replacements[i] = Character.toString(Character.toLowerCase((char) i));

        removeCharacters(_filter);
    }

    /**
     *
     * @param _input input from file
     * @return cleaned input
     */
    public List<String> clean(List<String> _input) {

        List<String> clean_input = new ArrayList<String>();

        for (String line : _input) {
            String[] words = line.split("\\s+");

            StringBuilder sb  = new StringBuilder(line.length());
            for (String word : words) {
                word = swapCharacters(word);
                if (word.length() > 0) {
                    if (sb.length() > 0)
                        sb.append(' ');
                    sb.append(word.trim());

                }
            }
            clean_input.add(sb.toString());
        }

        return clean_input;
    }

    /**
     *
     * @param _word word to have characters removed
     * @return cleaned word
     */
    private String swapCharacters(String _word) {
        StringBuilder sb = new StringBuilder(_word.length());
        for(int i = 0; i < _word.length(); i++)
            sb.append(replacements[_word.charAt(i)]);
        return sb.toString();
    }

    /**
     *
     * @param _filter list of characters to remove
     */
    public void removeCharacters(List<Character> _filter) {
        for (char c : _filter) {
            replacements[c] = " ";
        }

    }
}
